/*
* EP-Tracing System : a tool to Trac mountain hike.           .kNNx
* Copyright (C) 2009  Raffaello Brondi                         'KNNNk
*                                                             ,XNNNl
* This program is free software:                             ,XNN0'
* you can redistribute it and/or modify                     ,XNNx.
* it under the terms of the                                'XNNl
* GNU General Public License                              .KNN:                     ..
* as published by                                        .0NNo                    .ONNO.
* the Free Software Foundation,                          xNNN.  :dkd,            .KNNNN;
* either version 3 of the License, or                   :NNNK ;XNNNNNo          ,KNNNX:
* (at your option) any later version.                   xNNNNcXNNNNNNk         cNNNNx.
*                                                       ,XNNNNNNNNNNX'       .xNNNK;
*                                                        .lKNNNNNNNK'       :KNNNo
*                                                          ;kNNNNNKxl;'.'l0NNNk.
* This program is distributed in                          .'  .:xXNNNNNNNNNNNO,
* the hope that it will be useful,                       :NNXd'   ,oONNNNNNk,
* but WITHOUT ANY WARRANTY;                              xNNNNN0c.    .;;,.
* without even the implied warranty of                   kNNNNNNNXd'
* MERCHANTABILITY or                                     xNNNNNNNNNNOc.
* FITNESS FOR A PARTICULAR PURPOSE.                      xNNNNNNNNNNNNXd.
* See the GNU General Public License                     ONNNN0.,o0NNNNNNl
* for more details.                                      0NNNNd    kNNNNNX.
*                                                        KNNNNl   oNNNNNXc
*                                                       .XNNNN:  dNNNNNx.
* You should have received a copy                       .XNNNN; lNNNNO'
* of the GNU General Public License                     .XNNNN; 'dxo'
* along with this program.                              .XNNNN;
* If not, see <http://www.gnu.org/licenses/>.            0NNNN:
*                                                        kNNNNc
*                                                        oNNNNd
*                                                        ;NNNN0
* Contact: rbrondi@gmail.com                             .XNNNN;
*                                                         kNNNNd
*                                                         .coo:.
*/

package EP.GPS;

import EP.UI.Trace.TracerState;
import javax.microedition.location.Location;
import javax.microedition.location.LocationListener;
import javax.microedition.location.LocationProvider;
import javax.microedition.location.QualifiedCoordinates;

/**
 *
 * @author Raffaello Brondi
 */
public class GPSDataWrapper implements LocationListener {
    private TracerState ts;
    private LocationProvider locP;
    private boolean initialized = false;
    private boolean firstUpdate = false;

    public GPSDataWrapper() {
        this.initialized = false;
    }

    public void initializeGDW(LocationProvider lp) {
        this.locP = lp;
        this.locP.setLocationListener(this, -1, 0, 0);
        this.initialized = this.locP != null;
    }

    public boolean isInitialized() {
        return this.initialized;
    }

    public synchronized void setTracerState(TracerState ts) {
        this.ts = ts;
        if (this.initialized) {
            this.providerStateChanged(this.locP, this.locP.getState());
        }
    }

    public synchronized void locationUpdated(LocationProvider arg0, final Location loc) {
        System.out.println("Location Updated");

        if(!this.firstUpdate){
            this.providerStateChanged(this.locP, this.locP.getState());
            this.firstUpdate = true;
        }

        if (loc != null && loc.isValid()) {
            new Thread() {

                public void run() {
                    if (loc != null && loc.isValid()) {
                        QualifiedCoordinates coord = loc.getQualifiedCoordinates();
                        ts.setCoord(coord);
                        ts.repaint();
                    } else {
                        ts.setTracerState(LocationProvider.TEMPORARILY_UNAVAILABLE);
                    }
                }
            }.start();
        }
    }

    public synchronized void providerStateChanged(LocationProvider location, final int newState) {
        new Thread() {
            public void run() {
                ts.setTracerState(newState);
            }
        }.start();
    }

    public void stop() {
        this.locP.setLocationListener(null, -1, 0, 0);
        this.initialized = false;
    }
}
